import Vue from 'vue'
import VueRouter from 'vue-router'
// 引入login组件
import Login from '../components/Login'
import Home from '../components/Home'
import Welcome from '../components/Welcome'
import Welcomeqian from '../components/Welcomeqian'
import UserList from '../components/admin/UserList'
import test from "../components/test";
import firstmain from "../components/firstmain";
import Production from '../components/admin/Production'
import CarList from '../components/admin/CarList'
import Bus from '../components/admin/Bus'
import Qian from '../components/admin/Qian'
import busqian from '../components/busqian'
import meqian from '../components/meqian'
import yudingqian from '../components/yudingqian'
import zf from '../components/zf'
import Jiaofei from '../components/admin/Jiaofei'
Vue.use(VueRouter)

const routes = [
  {
    path:'/',
    redirect:'/login'
  },
  {
    path:'/login',
    component: Login
  },

    {
    path:'/test',
    component: test,

    redirect: '/welcome',
    children: [
      { path:'/welcome', component: Welcome},
      { path:'/user', component: UserList},
      { path:'/car', component: CarList},
      { path:'/production', component: Production},
      { path:'/bus', component: Bus},
      { path:'/qian', component: Qian},
      { path:'/jiaofei', component: Jiaofei},
    ],
  },
  {
    path:'/firstmain',
    component: firstmain,
    redirect: '/welcomeqian',
    children: [
      { path:'/busqian', component: busqian},
      { path:'/welcomeqian', component: Welcomeqian},
      { path:'/meqian', component: meqian},
      { path:'/yudingqian', component: yudingqian},
      { path:'/zf', component: zf},


    ],

  },

]

const router = new VueRouter({
  routes
})// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
    // to:将要访问的路径
    // from:从哪里访问的路径
    // next:之后要做的任务，是一个函数
    //    next（）放行， next（'/URL'）强制跳转的路径。
    if (to.path == '/login') return next();// 访问路径为登录
    // 获取flag
    const flagStr = window.sessionStorage.getItem("flag");// session取值
    if (!flagStr) return next('/login');// 没登录去登录
    next();
})

export default router// 暴露出去
